<?php

class Modeles extends Table
{

    // Ecrit le tableau des modeles actifs
    public static function Tableau($db)//-------------------------------------------------
	{
/*
    {
        $sql="SELECT mod_id, mod_code".
            ' FROM modeles';
*/
        $sql = "SELECT mod_id, mod_code, COUNT(mac_id) as mod_mac_nb
				FROM modeles
				LEFT JOIN machines 
				ON modeles.mod_id=machines.mac_mod_id
				GROUP BY mod_id";
        $champs = array ('mod_code', 'mod_mac_nb');
        $msg_vide = LNG_MOD_VIDE;
		return parent::Tableau($db, $sql, $champs, $champs, $msg_vide, True);
    }//------------------------------------------------------------------Tableau

    // Ecrit le tableau des modeles actifs
    public static function Tableau_rapide($db, $critere)//-------------------------------------------------
    {
        $sql="SELECT mod_id, mod_code".
            ' FROM modeles'.
            " WHERE mod_code LIKE '%$critere%'";
        $champs = array ('mod_code');
        $msg_vide = LNG_MOD_VIDE;
		return parent::Tableau($db, $sql, $champs, $msg_vide, True);
    }//------------------------------------------------------------------Tableau

    public static function Select($db, $idexclu)
    {
        $sql="SELECT mod_id, mod_code".
            ' FROM modeles'.
            " WHERE mod_id!=$idexclu".
            ' ORDER BY mod_code ASC';
        if ($result = $db->query($sql))
		{
			if ( $result->num_rows<1 )
				return False;
			$retour = Array();
			while ( $row = $result->fetch_assoc() )
				$retour[] = Array (	$row['mod_id'],
									$row['mod_code']);
			return $retour;
		}
		return False;
    }
   

public static function link_clean(){
	if ( droit_test(NIV_ADMI, False) )
		echo "<a href=\"Javascript:clean_mod('".LNG_MOD_CLEAN_CONFIRM."','confirm=1')\">".LNG_MOD_CLEAN."</a><br/>";
}

}

class Modele extends Enregistrement
{
	// Constructeur
	function __construct($db)//-------------------------------------------------
	{
		$this->defaut = Array('mod_id' 	=> -1,
						'mod_code' 		=> '');						
		parent::__construct($db, 'modeles', 'mod_id');
	}//--------------------------------------------------------------__construct

    
    function Supprimer($remplacant){
		Machines::UpdateMod($this->db, $this->attribut['mod_id'], $remplacant); 
		$sql = "DELETE FROM modeles WHERE mod_id={$this->id}";
		return ( $result = $this->db->query($sql) );
    }

	function NomFormat(){
		return parent::NomFormat('mod_code');
	}

	function Traitement(&$erreur){
		if ( isset( $_POST['maj_infos'] ) )
		{
			$this->LirePost();
			$this->Verification();
			if ( $this->VerifOk() )
				if ( $this->Ecrire() )
				{
					$_SESSION[SES_STATUT] = LNG_STA_MAJ_0;
					$id = $this->cle();
				}
				else
					$_SESSION[SES_STATUT] = LNG_STA_ERREUR.LNG_STA_MAJ_1;
		}
		elseif ( isset( $_POST['maj_sup'] ) )
		{
			if ( isset( $_POST['confirmation'] ) ){
				if ($_POST['remplacant']==-1) {
					$erreur['remplacant'] = LNG_UTI_REMPLACANT_AIDE;
				}
				else {
					if ( $this->Supprimer($_POST['remplacant']) ){
						$_SESSION[SES_STATUT] = LNG_STA_UTI_SUP_0;
						header('location:modeles.php');
					}
					else{
						$_SESSION[SES_STATUT] = LNG_STA_ERREUR.LNG_STA_UTI_SUP_1;
					}
				}
			}
			else
				$erreur['confirmation'] = LNG_UTI_CONFIRMATION_AIDE;
		}		
	}//-------------------------------------------------------------------------

	
	// Verification des attributs via la base
	function Verification()//---------------------------------------------------
	{
		// Verif de l'unicité du code
		$this->erreurs['mod_code'] = '';
		if ( trim($this->attribut['mod_code']) == '' )
			$this->erreurs['mod_code'] = LNG_CHAMP_OBLIGATOIRE;
		else
		{
			$sql = 'SELECT mod_code'
				.' FROM modeles'
				." WHERE mod_code='{$this->attribut['mod_code']}' AND mod_id!={$this->attribut['mod_id']}";
			if ( $result = $this->db->query($sql) )
			{
				if ( ($this->attribut['mod_id']==-1 && $result->num_rows>0) || ($this->attribut['mod_id']!=-1 && $result->num_rows>0) )
					$this->erreurs['mod_code'] .= LNG_UTI_CODEEXISTANT;
			}
		}		
		return $this->erreurs;
	}//-------------------------------------------------------------Verification

	function Contenu($erreur,$onglet=0, $edition=False)
	{
		switch ($onglet)
		{
			case 0:
				$h4 = LNG_H4_INFORMATIONS;
				if ( droit_modifier(GR_MOD_MODIF, $edition) )
					$h4 .= Liens::ModModifier($this->cle());
?>
<form method='post' action='modele.php?<?="id={$this->cle()}&amp;edition=$edition"?>'>
    <?php
		dohtml_h4($h4);
		$this->ChampText('mod_code', $edition);
		if ($edition)
			dohtml_formbouton('labelagauche', 'Mettre à jour', 'maj_infos', 'Annuler');
?>
</form>


				<?php
				if ($this->cle() != -1 && $_SESSION[SES_USER_NIVEAU]>=GR_MOD_SUPPR)
				{
				?>

				<form method='post' action='modele.php?<?="id={$this->cle()}&amp;edition=$edition"?>'>

				<?php	
						dohtml_h4(LNG_H4_SUPPRESSION);
						dohtml_formcheck('labelagauche',
										'confirmation',
										LNG_UTI_CONFIRMATION,
										LNG_UTI_CONFIRMATION_AIDE,
										$erreur['confirmation']);
						dohtml_formselect('labelagauche',
										'remplacant',
										LNG_UTI_REMPLACANT,
										LNG_UTI_REMPLACANT_AIDE,
										modeles::select($this->db, $this->attribut['mod_id']),
										True,
										-1,
										$erreur['remplacant']);
						dohtml_formbouton('labelagauche', 'Supprimer', 'maj_sup');
				?>
				</form>

				<?php
				}			



			break;
			case 1:
				Machines::Tableau_Mod($this->db, $this->cle());
			break;
		}
	}
}

